package com.ruoyi.cat.service.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import com.ruoyi.cat.domain.CatUserbasicTable;
import com.ruoyi.cat.domain.DTO.AwesomeTeammateDTO;
import com.ruoyi.cat.domain.DTO.MyTeamDTO;
import com.ruoyi.cat.mapper.CatUserbasicTableMapper;
import com.ruoyi.cat.service.ICatTradedetailTableService;
import com.ruoyi.cat.service.ICatUserbasicTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-24
 */
@Service
public class CatUserbasicTableServiceImpl implements ICatUserbasicTableService
{
    @Autowired
    private CatUserbasicTableMapper catUserbasicTableMapper;

    @Autowired
    private ICatTradedetailTableService catTradedetailTableService;

    /**
     * 查询用户信息
     * 
     * @param id 用户信息主键
     * @return 用户信息
     */
    @Override
    public CatUserbasicTable selectCatUserbasicTableById(Long id)
    {
        return catUserbasicTableMapper.selectCatUserbasicTableById(id);
    }

    /**
     * 查询用户信息列表
     * 
     * @param catUserbasicTable 用户信息
     * @return 用户信息
     */
    @Override
    public List<CatUserbasicTable> selectCatUserbasicTableList(CatUserbasicTable catUserbasicTable)
    {
        return catUserbasicTableMapper.selectCatUserbasicTableList(catUserbasicTable);
    }

    /**
     * 新增用户信息
     * 
     * @param catUserbasicTable 用户信息
     * @return 结果
     */
    @Override
    public int insertCatUserbasicTable(CatUserbasicTable catUserbasicTable)
    {
        return catUserbasicTableMapper.insertCatUserbasicTable(catUserbasicTable);
    }

    /**
     * 修改用户信息
     * 
     * @param catUserbasicTable 用户信息
     * @return 结果
     */
    @Override
    public int updateCatUserbasicTable(CatUserbasicTable catUserbasicTable)
    {
        return catUserbasicTableMapper.updateCatUserbasicTable(catUserbasicTable);
    }

    /**
     * 批量删除用户信息
     * 
     * @param ids 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteCatUserbasicTableByIds(Long[] ids)
    {
        return catUserbasicTableMapper.deleteCatUserbasicTableByIds(ids);
    }

    /**
     * 删除用户信息信息
     * 
     * @param id 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteCatUserbasicTableById(Long id)
    {
        return catUserbasicTableMapper.deleteCatUserbasicTableById(id);
    }

    @Override
    public CatUserbasicTable selectCatUserbasicTableByOpenId(String openId) {
        return catUserbasicTableMapper.selectCatUserbasicTableByOpenId(openId);
    }

    @Override
    public CatUserbasicTable selectCatUserbasicTableByReferenceNum(String referenceNum) {
        return catUserbasicTableMapper.selectCatUserbasicTableByReferenceNum(referenceNum);
    }

    @Override
    public int countByReferenceId(Long userId) {
        int data =  catUserbasicTableMapper.countByReferenceId1(userId);
        return data;
    }

    @Override
    public List<MyTeamDTO> getMyTeam(Long referenceId) {
        List<MyTeamDTO> list = catUserbasicTableMapper.getMyTeam(referenceId);
        list.stream().forEach(e->{
            e.setMember(catUserbasicTableMapper.countByReferenceId1(e.getId()));
            AwesomeTeammateDTO awesomeTeammateDTO = catTradedetailTableService.countByReferenceId(e.getId());
            if(awesomeTeammateDTO!=null){
                if (awesomeTeammateDTO.getTradeNum().compareTo(BigDecimal.ZERO) != 0) {
                    e.setRevenueAmount(awesomeTeammateDTO.getTradeNum());
                }else{
                    e.setRevenueAmount(BigDecimal.ZERO);
                }
            }
        });
        return list;
    }



    @Override
    public CatUserbasicTable getUnionId(String unionid) {
        return catUserbasicTableMapper.getUnionId(unionid);
    }

    @Override
    public List<CatUserbasicTable> countByReferenceData(Long referenceId) {
        return catUserbasicTableMapper.countByReferenceId(referenceId);
    }
}
